<?php
namespace app\api\validate;

use app\lib\exception\PapameterException;

class OrderPlace extends BaseValidate{

    protected $rule = [
        'products' => 'checkProducts'
    ];

    protected $singleRule = [
        'product_id' => 'require|isPositiveInteger',
        'count' => 'require|isPositiveInteger',
    ];


    protected function checkProducts($values){
        if(!is_array($values)){
            throw new PapameterException([
                'msg' => '商品参数不正确'
            ]);
        }
        if(empty($values)){
            throw new PapameterException([
                'msg' => '商品列表不能为空'
            ]);
        }
        foreach($values as $value){
            $this->checkProduct($value);
        }
        
    }

    //验证数组里的每个子项
    protected function checkProduct($value){
        $validate = new BaseValidate($this->singleRule);
        $result = $validate->check($value); //调用验证器
        if(!result){
            throw new PapameterException([
                'msg' => '商品列表参数错误'
            ]);
        }
    }
}

?>